import {ElMessage} from "element-plus";
import router from "@/router";

// 设置过期时间，为1000 * 60 * 60，为一个小时
export const expiration = 1000 * 60 * 60;

/**
 * 判断当前的localStroage是否过期
 */
export const setToExpireLocalStroage = (expiration) => {
    // 记录登录的时间
    let expire = localStorage.getItem('expiration')
    // 判断当前时间是否大于预计时间，当超时时删除system-user
    if((Date.now() - expire) > expiration) {
        removeLocalStroage()
        ElMessage.error('登录信息已过期，请重新登录')
        router.push('/login')
    }
}

/**
 * 获取到当前的localStorage
 */
export const getLocalStroage = () => {
    // 获取localStorage之前先判断时间是否过期
    setToExpireLocalStroage(expiration);
    return JSON.parse(localStorage.getItem('system-user')) || {};
}

/**
 * 删除当前的localStorage
 */
export const removeLocalStroage = () => {
    localStorage.removeItem('system-user')
}